﻿<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0, user-scalable=no" />
<link rel="shortcut icon" href="/Images/ico/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="/css/swiper.min.css" />
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<link type="text/css" rel="stylesheet" href="/layui/css/layui.css"  media="all">
<script type="text/javascript" src="/layui/layui.js" charset="utf-8"></script>
<script src="/js/jquery.min.js" type="text/javascript"></script>
<script src="/js/swiper.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/js/custom.js"></script>
<script type="text/javascript" src="/js/layui-post.js"></script>
<style>
.white-color{background: #fff;border-radius: 7px;}
.lists div.l img {border-radius: 10px; width: 60px;  height: 60px;}
.lists div.r {font-size: .9rem;}
</style>
</head>
<body style="background: #F0F0F0;padding: 10px;">
    <div class="ng-scope">
        <header class="ng-scope" id="gwc">
            <h4 class="title-order">
                <a class="title-top" href="javascript:window.history.back();"></a>
                <a class="title-center">付款</a>
                <a class="title-right" href="/index"></a>
            </h4>
        </header>
        <p class="orderno ng-binding ng-scope white-color" style="margin-top: 50px;">订单编号：<span id="orderno"></span><span style="text-align:right;float: right;color: red;font-size: 16px;" id="djs"></span></p>
        <div class="top-pay ng-scope white-color peisong" style="margin: 10px 0;display: none;"></div>
        <div class="paytype">
            <h4>使用积分抵扣现金</h4>
        </div>
        <div class="coupon white-color" id="jifen" style="background: #fff;">
            <input type="checkbox" id="chechedBox" style="vertical-align: middle;"/> 账户共有<span id="interNum">0</span>个积分，本次使用 <input type="number" class="number" id="userInterNum" max="225" min="1" value="0" /> 个 <span class="red">￥<span class="red" id="discount">0.00</span></span>
        </div>
        <p class="nocoupon ng-scope ng-hide">无可用积分</p>
        <div class="paytype">
            <h4>商品费用</h4>
        </div>
        <div class="bottom-pay ng-scope white-color" style="background: #fff;">
            <section class="prolist_section" style="padding-bottom: 0px;">
                <ul id="prolist" class="lists" style="padding: 10px;"></ul>
            </section>
        </div>

        <div class="paytype ng-scope" id="paymethod">
            <h4>支付方式</h4>
            <ul class="white-color">
                <li class="wxpay" ng-click="payselect('wxpay')">
                    <img src="/img/payok.jpg" alt="" class="show">
                    <img src="/img/payno.jpg" alt="" class="hide">
                    微信支付
                </li>
            </ul>
        </div>
        <div class="clear ng-scope" style="height: 90px;"></div>

        <!-- 底部结算导航 -->
        <div class="cart cart-pay ng-scope" id="paybtn">
            <div class="r" id="submit"><a href="javascript:;">付 款</a></div>
            <div class="l ng-binding c">
                实付 ￥<label id="totalmoney" data-value="0">0</label>
            </div>
        </div>
        <!-- /底部结算导航 -->
    </div>
</body>
<script>
$.ajaxSetup({contentType: "application/json; charset=utf-8"});
var userInfo = $.parseJSON(sessionStorage.getItem("userInfo"));
var createTime = null,status = 0;
var sendIntegra = 0,integra = 0;
layui.use(['layer','form','element','util'], function(){
    $.post("/cateringUser/selectOne",JSON.stringify({opendId:sessionStorage.getItem("opendId")}), function (res) {
        if(res.data.integral/1 > 0){
            integra = res.data.integral
            $("#interNum").text(res.data.integral)
            $("#discount").text(res.data.integral/100)
            $("#userInterNum").val(res.data.integral).attr("max",res.data.integral).change(function(){
                if($(this).val()/1 > $(this).attr("max")/1) {
                    layer.msg("输入数量不能超过可用积分数量",{icon:0,time:1500},function(){
                        $("#userInterNum").val(res.data.integral)
                        $("#discount").text($("#userInterNum").val()/100)
                    })
                }else{
                    $("#discount").text($(this).val()/100)
                    changeTotalMoney();
                }
            })
        }else{
            $("#jifen").addClass("ng-hide")
            $(".nocoupon").removeClass("ng-hide")
        }
    })
    $.post("/cateringOrder/selectOrder",JSON.stringify({sysId:getQueryString("orderId")}),function(res){
        $("#prolist").html("")
        for(var i in res.data){
            sendIntegra = res.data[i].sendIntegra/1
            if(res.data[i].status == 2) {
                $("#paybtn").hide()
                $("#paymethod").hide()
                $("#djs").html("已取消")
            }else{
                var date = new Date(res.data[i].createTime.replaceAll("-","/"))
                var min = date.getMinutes()
                date.setMinutes(min + 15)
                createTime = date;status = res.data[i].status;
                timer(createTime);
                setInterval(function(){timer(createTime)}, 1000);
            }
            $("#orderno").html(res.data[i].orderNo);
            if(res.data[i].sendType == 1){
                $(".peisong").show().append(
                    '<p class="ng-binding" style="line-height: 30px;"><div style="float: left;width: 20%;height: 30px;line-height: 30px;">收货信息：</div><div style="float: right;width: 80%;text-align: right;height: 30px;line-height: 30px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis; ">'+res.data[i].person+' '+res.data[i].telPhone+'</div></p>' +
                    '<p class="ng-binding" style="line-height: 30px;"><div style="float: left;width: 20%;height: 30px;line-height: 30px;">配送地址：</div><div style="float: right;width: 80%;text-align: right;height: 30px;line-height: 30px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis; ">'+res.data[i].detail+'</div></p>' +
                    '<p class="ng-binding" style="line-height: 30px;"><div style="float: left;width: 20%;height: 30px;line-height: 30px;">订单备注：</div><div style="float: right;width: 80%;text-align: right;height: 30px;line-height: 30px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis; ">'+res.data[i].remark+'</div></p><div style="clear:both;"></div>'
                )
            }else{
                $(".peisong").show().append(
                    '<p class="ng-binding" style="line-height: 30px;"><div style="float: left;width: 20%;height: 30px;line-height: 30px;">自取时间：</div><div style="float: right;width: 80%;text-align: right;height: 30px;line-height: 30px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis; ">'+res.data[i].sendTime+'</div></p>' +
                    '<p class="ng-binding" style="line-height: 30px;"><div style="float: left;width: 20%;height: 30px;line-height: 30px;">预留电话：</div><div style="float: right;width: 80%;text-align: right;height: 30px;line-height: 30px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis; ">'+res.data[i].remark+'</div></p><div style="clear:both;"></div>'
                )
            }
            for(var j in res.data[i].cartList){
                let yxgg = !isEmpty(res.data[i].cartList[j].spacesName) ? '<p class="t" style="font-size: 10px;margin-top: 6px;">已选规格：'+res.data[i].cartList[j].spacesName+'</p>': ""
                $("#prolist").append(
                    '<li price="'+res.data[i].cartList[j].goodsPrice+'" goodnum="'+res.data[i].cartList[j].goodsNum+'" class="on" goodsId="'+res.data[i].cartList[j].goodsId+'" internum="'+res.data[i].cartList[j].interNum+'">' +
                    '<div class="l" style="width: 20%;">' +
                    '<a><img src="'+res.data[i].cartList[j].goodsImg+'" class="img-responsive" alt="..." /></a>' +
                    '</div>' +
                    '<div class="r" style="width: 80%;">' +
                    '<p class="t">'+res.data[i].cartList[j].goodsName+'</p>' +
                    '<p class="price" style="float:none;">￥'+res.data[i].cartList[j].goodsPrice+'</p>' +
                    yxgg +
                    // '<div class="jifen">送积分：'+res.data[i].cartList[j].interNum+'</div>' +
                    '<div class="jifen">x'+res.data[i].cartList[j].goodsNum+'</div>' +
                    '</div>' +
                    '</li>'
                )
            }
            CountNumPrice();
        }
    })
})
function CountNumPrice() {
    var totalcartnumber = 0;//数量
    var totalmoney = 0;//价格
    var totalpoints = 0;//积分
    $(".lists .on").each(function (i, model) {
        var objLi = $(this);
        var number = parseInt($(objLi).attr("goodnum"));
        var price = parseFloat($(objLi).attr("price"));
        var inter = parseFloat($(objLi).attr("interNum"));
        totalcartnumber = totalcartnumber + number;
        totalmoney = totalmoney + (number * price);
        totalpoints = totalpoints + (number * inter);
    });
    $("#totalmoney").attr("data-value",totalmoney)
    if($("#chechedBox").prop("checked")) $("#totalmoney").text((totalmoney-parseFloat($("#discount").text())).toFixed(2));
    else $("#totalmoney").text(totalmoney.toFixed(2));
    $("#totalcartnumber").text(totalcartnumber);
    $("#points").text(totalpoints);
}
$("#chechedBox").click(function(){
    changeTotalMoney();
})
//改变积分和实付金额
function changeTotalMoney(){
    if($("#chechedBox").prop("checked")) {
        if((parseFloat($("#totalmoney").attr("data-value")) - parseFloat($("#discount").text())) <= 0){
           // $("#discount").text(($("#discount").text()/1-$("#totalmoney").attr("data-value")/1)*100)
            $("#discount").text(parseFloat($("#totalmoney").attr("data-value")).toFixed(2))
            $("#userInterNum").val($("#totalmoney").attr("data-value")*100)
            $("#totalmoney").text("0.00");
        }else{
            $("#totalmoney").text(($("#totalmoney").attr("data-value")-parseFloat($("#discount").text())).toFixed(2));
        }
    }
    else $("#totalmoney").text(parseFloat($("#totalmoney").attr("data-value")).toFixed(2));
}
//支付
$("#submit").click(function(){
    //支付之前查询一下改订单的状态是否支付
    $.post("/cateringOrder/selectOrder",JSON.stringify({sysId:getQueryString("orderId")}),function(res){
        for(var i in res.data){
            if(res.data[i].status == 1){
                layer.msg("订单已支付！",{icon:0,time:1500})
            }else{
                const shopNo = $("#orderno").html();
                let shopIntegra = 0;
                if($("#chechedBox").prop("checked")) shopIntegra = $("#userInterNum").val();
                if($("#totalmoney").html()/1 == 0){
                    $.post("/cateringOrder/updateStatus",JSON.stringify({sysId:getQueryString("orderId"),status:1,useIntegra:shopIntegra,payMoney:"0.00",orderStatus:"ORDER_STATUS_02,ORDER_STATUS_20"}),function(){
                        $.post("/cateringUser/update",JSON.stringify({sysId:userInfo.sysId,integral:(integra - shopIntegra + sendIntegra)}),function(res){
                            if(shopIntegra > 0)$.post("/cateringIntegral/insert",JSON.stringify({orderId:getQueryString("orderId"),userId:userInfo.userId,integralNum:shopIntegra,integralType:2}))
                            if(sendIntegra > 0)$.post("/cateringIntegral/insert",JSON.stringify({orderId:getQueryString("orderId"),userId:userInfo.userId,integralNum:sendIntegra,integralType:1}))
                        })
                        if(res.success){
                            layer.msg(res.msg,{icon:1,time:1500},function(){
                                window.location.href = "/order"
                            })
                        }
                    })
                }else{
                    sessionStorage.setItem("shopNo",shopNo);
                    var url = getHttp()+"/wxpay/pay/cateringpay.html?shopNo="+shopNo+"&shopMoney="+parseInt($("#totalmoney").html()/1*100)+"&userId="+userInfo.userId+"&orderId="+getQueryString("orderId")+"&shopIntegra="+shopIntegra;
                    var appid = "wx1c9f69395c9b34ec";
                    url = encodeURIComponent(url);
                    window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri=" + url + "&response_type=code&scope=snsapi_userinfo&state=shzz_portal_push#wechat_redirect";
                }
            }
        }
    })
})
function timer(time) {
    var endtime = new Date(time); //设置结束的时间
    var now = new Date();//设置当前时间
    var t = endtime.getTime() - now.getTime();//得到结束与当前时间差 ： 毫秒
    if (t > 0) {
        var tian = Math.floor(t / 1000 / 60 / 60 / 24);//得到剩余天数
        var h = Math.floor(t / 1000 / 60 / 60 % 24);//得到还剩余的小时数（不满一天的小时）
        var m = Math.floor(t / 1000 / 60 % 60);//得到分钟数
        var s = Math.floor(t / 1000 % 60); //得到的秒数
        var str = m + " : " + s;
        $("#djs").html(str)
    } else {
        clearInterval(timer); //这里可以添加倒计时结束后需要执行的事件
        //倒计时结束修改订单状态；
        if(status == 0){
            status = 2
            $.post("/cateringOrder/update",JSON.stringify({sysId:getQueryString("orderId"),status:2,orderStatus:"ORDER_STATUS_00"}),function(){
                $("#djs").html("已取消");
                $("#paybtn").hide()
                $("#paymethod").hide()
                //新增一条取消记录
                $.post("/cateringOrderLog/insert",JSON.stringify({orderId:getQueryString("orderId"),orderStatus:"ORDER_STATUS_00"}))
            })
        }
    }
}
</script>
</html>
